package jucTest;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
       /* CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("异步执行线程");
        }, Executors.newSingleThreadExecutor());
        System.out.println("1111111");
        future.get();*/
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("进行线程操作");
           // int i = 10 / 0;
            return 1024;
        });
        future.whenComplete((u, t) -> {
            System.out.println(u);
            System.out.println(t);
        }).exceptionally(
                (e) -> {
                    System.out.println("exceptionally->"+e);
                    return 404;
                }
        );
    }
}
